package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.codehaus.plexus.util.StringUtils;
import zhang.lao.build.kit.LogKit;
import zhang.lao.build.mybatis.generator.util.StringUtil;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.BeanMapTool;
import zhang.lao.build.tool.MapTool;
import zhang.lao.dao.base.WnFarmDao;
import zhang.lao.dao.base.WnMachineDeviceDao;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.dao.base.WnMachineDeviceLinkDao;
import zhang.lao.pojo.WnConstant;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResultEnum;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.HttpResult;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.service.console.SysSequenceFactory;

/**
* 
* @author 
* @create 
**/
 @Service
public class WnMachineDeviceService{
 	@Resource
	private WnFarmDao wnFarmDao;
	@Resource
	private WnMachineDeviceDao wnMachineDeviceDao;
	@Resource
	private WnMachineDeviceLinkDao wnMachineDeviceLinkDao;
	@Resource
	private SysSequenceFactory sysSequenceFactory;
	public String add(){
		return "console/wnMachineDevice/wnMachineDevice_form";
	}

	public String edit(ModelMap modelMap,java.lang.Integer id){
			modelMap.put("wnMachineDevice", wnMachineDeviceDao.selectByPrimaryKey(id));
		return "console/wnMachineDevice/wnMachineDevice_form";
	}

	public String list(){
		return "console/wnMachineDevice/wnMachineDevice_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq, ConsoleContext consoleContext){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
        ControllerQueryTool.setWnMachineDeviceCriteria(bootGridReq.getQuery(),wnMachineDeviceExample.createCriteria()).andFarmIdIn(consoleContext.getFarms()).andDeviceTypeEqualTo(WnConstant.DEVICE_TYPE_windows_auto_machine);
		List<WnMachineDevice> wnMachineDeviceList = wnMachineDeviceDao.selectByExample(wnMachineDeviceExample);
		List<JSONObject> list = BeanMapTool.objectsToJsonObjects(wnMachineDeviceList);
			if(list!=null){
				for (JSONObject jsonObject : list) {
					jsonObject.put("farmName",wnFarmDao.selectByPrimaryKey(jsonObject.getString("farmId")).getName());
				}
			}
		return new BootStrapGridResp(page.getTotal(),list);
	}


	@Transactional
	public HttpResult save(String formObjectJson,String machineId,ConsoleContext consoleContext){
		WnMachineDevice wnMachineDevice= JSON.parseObject(formObjectJson,WnMachineDevice.class);
			Integer id=wnMachineDevice.getId();
		if (id!=null) {
			wnMachineDevice.setDeviceNumber(null);
			wnMachineDevice.setUpdateTime(new Date());
			wnMachineDevice.setUpdateUser(consoleContext.getStrUserId());
			wnMachineDeviceDao.updateByPrimaryKeySelective(wnMachineDevice);

		}else{
			wnMachineDevice.setId(sysSequenceFactory.getSequence("wn_machine_device"));
			WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
			wnMachineDeviceExample.createCriteria().andDeviceNumberEqualTo(wnMachineDevice.getDeviceNumber());
			if(wnMachineDeviceDao.countByExample(wnMachineDeviceExample)>0){
				return HttpResultUtil.buildError(HttpResultEnum.DEVICE_NUMBER_ERR);
			}
			wnMachineDevice.setCreateUser(consoleContext.getStrUserId());
			wnMachineDevice.setCreateTime(new Date());
			wnMachineDeviceDao.insertSelective(wnMachineDevice);
			if(StringUtils.isNotBlank(machineId)){
				WnMachineDeviceLink wnMachineDeviceLink = new WnMachineDeviceLink();
				wnMachineDeviceLink.setId(UUIDTool.getUUID());
				wnMachineDeviceLink.setMachineId(machineId);
				wnMachineDeviceLink.setDeviceId(wnMachineDevice.getId());
				wnMachineDeviceLinkDao.insert(wnMachineDeviceLink);

			}
		}
		return HttpResultUtil.buildSuccess();

	}

	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
			WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
			wnMachineDeviceLinkExample.createCriteria().andDeviceIdEqualTo(Integer.parseInt(id));
			wnMachineDeviceLinkDao.deleteByExample(wnMachineDeviceLinkExample);
			wnMachineDeviceDao.deleteByPrimaryKey(Integer.parseInt(id));
		}
		return HttpResultUtil.buildSuccess("删除成功(仅解除与农机关联)!");
	}
}
